package com.healthcloud.controller.modules.patient;

import com.healthcloud.entity.*;
import com.healthcloud.entity.PatientRecord.HcDrugEntity;
import com.healthcloud.entity.PatientRecord.HcMedicalEntity;
import com.healthcloud.entity.PatientRecord.HcMedicalEntityQh;
import com.healthcloud.entity.accompany.VitalCharacteristics;
import com.healthcloud.service.*;
import com.healthcloud.service.PatientRecord.HcDrugService;
import com.healthcloud.service.PatientRecord.HcMedicalService;
import com.healthcloud.service.PatientRecord.VitalCharacteristicsService;
import com.healthcloud.utils.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 用药
 */
@Controller
@RequestMapping("medical")
public class MedicalController {
    @Autowired
    private HcMedicalService hcMedicalService;

    @Autowired
    private HlDoctorService hlDoctorService;

    @Autowired
    private HcDrugService hcDrugService;

    @Autowired
    private VitalCharacteristicsService vitalCharacteristicsService;

    /**
     * 根据idCard查询用药信息
     */
    @ApiOperation(value = "查询用药信息", notes = "查询用药信息")
    @RequestMapping(value = "/medicalInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes medicalInfo(HcMedicalEntity entity,
                              PageParam pageParam) {

        List<HcMedicalEntity> list = hcMedicalService.queryList(entity, pageParam);
        int total = hcMedicalService.queryTotal(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", total);
        return new ApiRes(true, map, "", "");
    }

    @ApiOperation(value = "删除用药")
    @RequestMapping(value = "/deleteMedicalInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteBs(Long[] ids) {
        hcMedicalService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至用药信息新增页面
     */
    @ApiOperation(value = "用药信息新增页面", notes = "用药信息新增页面")
    @RequestMapping(value = "/toAddMedical", method = RequestMethod.GET)
    public String toAddMedical() {
        return "admin/archives/addMedicalInfo";
    }

    /**
     * 跳转至用药信息新增页面
     */
    @ApiOperation(value = "用药信息新增页面", notes = "用药信息新增页面")
    @RequestMapping(value = "/toAddVitalCharacteristics", method = RequestMethod.GET)
    public String toAddVitalCharacteristics() {
        return "admin/archives/addVitalCharacteristics";
    }

    /**
     * 跳转至用药信息更多页面
     */
    @ApiOperation(value = "用药信息更多页面", notes = "用药信息更多页面")
    @RequestMapping(value = "/toMoreMedical", method = RequestMethod.GET)
    public String toMoreMedical() {
        return "admin/archives/moreMedical";
    }



    /**
     * 新增用药信息
     */
    @ApiOperation(value = "新增用药信息", notes = "新增用药信息")
    @RequestMapping(value = "/addMedical", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addMedical(HcMedicalEntity entity) {
        if(StringUtils.isNull(ShiroUtils.getUserId())) {
            return ApiRes.failure("登录失效，请重新登录。");
        }
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());

        HcDrugEntity hcDrugEntity = hcDrugService.queryObject(entity.getMedicalId());
        entity.setMedicalName(hcDrugEntity != null ? hcDrugEntity.getName() : "");
        entity.setType(1);//页面添加默认为1
        entity.setDoctorId(doctor.getId());
        entity.setDoctorName(doctor.getDrName());
        hcMedicalService.save(entity);
        return ApiRes.SUCCESS;
    }

    /**
     * 跳转至用药信息修改页面
     */
    @ApiOperation(value = "用药信息修改页面", notes = "用药信息修改页面")
    @RequestMapping(value = "/toUpdateMedical", method = RequestMethod.GET)
    public String toUpdateMedical() {
        return "admin/archives/updateMedical";
    }

    /**
     * 修改用药信息
     */
    @ApiOperation(value = "修改用药信息", notes = "修改用药信息")
    @RequestMapping(value = "/updateMedical", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updateMedical(@ApiParam(required = true, name = "id", value = "用药信息id编号")
                                @RequestParam(value = "id") Integer id,
                                @ApiParam(required = true, name = "medicalId", value = "药名(药名对应的id)")
                                @RequestParam(value = "medicalId") Integer medicalId,
                                @ApiParam(required = true, name = "medicalFrequency", value = "频率")
                                @RequestParam(value = "medicalFrequency") String medicalFrequency,
                                @ApiParam(required = true, name = "numbers", value = "数量")
                                @RequestParam(value = "numbers") Double numbers,
                                @ApiParam(required = true, name = "medicalSpecs", value = "规格")
                                @RequestParam(value = "medicalSpecs") String medicalSpecs,
                                @ApiParam(required = true, name = "useType", value = "用法")
                                @RequestParam(value = "useType") Integer useType,
                                @DateTimeFormat(pattern = DateUtils.DATE_TIME_PATTERN)
                                @ApiParam(required = true, name = "recordTime", value = "开立时间")
                                @RequestParam(value = "recordTime") Date recordTime) {
        HcMedicalEntity entity = new HcMedicalEntity();
        HcDrugEntity hcDrugEntity = hcDrugService.queryObject(medicalId);
        entity.setId(id);
        entity.setMedicalId(medicalId);
        entity.setMedicalName(hcDrugEntity != null ? hcDrugEntity.getName() : "");
        entity.setMedicalSpecs(medicalSpecs);
        entity.setMedicalFrequency(medicalFrequency);
        entity.setNumbers(numbers);
        entity.setUseType(useType);
        entity.setRecordTime(recordTime);
        hcMedicalService.update(entity);
        return ApiRes.SUCCESS;
    }

    /**
     * 新增用药信息----药品下拉
     */
    @ApiOperation(value = "药品下拉列表", notes = "药品下拉列表")
    @RequestMapping(value = "/selectMedicalName", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes selectMedicalName(@RequestParam("medicalName") String medicalName,
                                    @RequestParam("acronym") String acronym) {
        List<HcDrugEntity> list = hcDrugService.queryAll(medicalName,acronym);
        return new ApiRes(true, list, "", "");
    }

    /**
     * 新增用药信息----规格下拉
     */
    @ApiOperation(value = "规格下拉列表", notes = "规格下拉列表")
    @RequestMapping(value = "/selectMedicalSpecs", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes selectMedicalSpecs(@ApiParam(name = "medicalName", value = "药名")
                                     @RequestParam(value = "medicalName", required = false) String medicalName,
                                     @RequestParam(value = "acronym", required = false) String acronym) {
        List<HcDrugEntity> list = hcDrugService.queryAll(medicalName,acronym);
        return new ApiRes(true, list, "", "");
    }


    //-------------------钱湖用药信息-----------------

    /**
     * 跳转至用药信息更多页面
     */
    @ApiOperation(value = "用药信息更多页面", notes = "用药信息更多页面")
    @RequestMapping(value = "/toMoreMedicalQh", method = RequestMethod.GET)
    public String toMoreMedicalQh() {
        return "admin/archives/moreMedicalQh";
    }



    /**
     * 根据idCard查询用药信息
     */
    @ApiOperation(value = "查询用药信息", notes = "查询用药信息")
    @RequestMapping(value = "/medicalInfoQh", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes medicalInfoQh(HcMedicalEntityQh entity,
                                PageParam pageParam) {

        List<HcMedicalEntity> list = hcMedicalService.queryListQh(entity, pageParam);
        int total = hcMedicalService.queryTotalQh(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", total);
        return new ApiRes(true, map, "", "");
    }

    /**
     * 跳转至用药信息新增页面
     */
    @ApiOperation(value = "用药信息新增页面", notes = "用药信息新增页面")
    @RequestMapping(value = "/toAddMedicalQh", method = RequestMethod.GET)
    public String toAddMedicalQh() {
        return "admin/archives/addMedicalInfoQh";
    }


    /**
     * 新增用药信息
     */
    @ApiOperation(value = "新增用药信息", notes = "新增用药信息")
    @RequestMapping(value = "/addMedicalQh", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addMedicalQh(HcMedicalEntity entity) {
        if(StringUtils.isNull(ShiroUtils.getUserId())) {
            return ApiRes.failure("登录失效，请重新登录。");
        }
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());

        /*HcDrugEntity hcDrugEntity = hcDrugService.queryObject(entity.getMedicalId());
        entity.setMedicalName(hcDrugEntity != null ? hcDrugEntity.getName() : "");*/
        entity.setType(1);//页面添加默认为1
        entity.setDoctorId(doctor.getId());
        entity.setDoctorName(doctor.getDrName());
        hcMedicalService.saveQh(entity);
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "删除用药")
    @RequestMapping(value = "/deleteMedicalInfoQh", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteQhBs(Long[] ids) {
        hcMedicalService.deleteBatchQh(ids);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转至用药信息修改页面
     */
    @ApiOperation(value = "用药信息修改页面", notes = "用药信息修改页面")
    @RequestMapping(value = "/toUpdateMedicalQh", method = RequestMethod.GET)
    public String toUpdateMedicalQh() {
        return "admin/archives/updateMedicalQh";
    }


    /**
     * 修改用药信息
     */
    @ApiOperation(value = "修改用药信息", notes = "修改用药信息")
    @RequestMapping(value = "/updateMedicalQh", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updateMedicalQh(@ApiParam(required = true, name = "id", value = "用药信息id编号")
                                  @RequestParam(value = "id") Integer id,
                                  @ApiParam(required = true, name = "medicalFrequency", value = "频率")
                                  @RequestParam(value = "medicalFrequency") String medicalFrequency,
                                  @ApiParam(required = true, name = "numbers", value = "数量")
                                  @RequestParam(value = "numbers") Double numbers,
                                  @ApiParam(required = true, name = "medicalSpecs", value = "规格")
                                  @RequestParam(value = "medicalSpecs") String medicalSpecs,
                                  @ApiParam(required = true, name = "useType", value = "用法")
                                  @RequestParam(value = "useType") Integer useType,
                                  @DateTimeFormat(pattern = DateUtils.DATE_TIME_PATTERN)
                                  @ApiParam(required = true, name = "recordTime", value = "开立时间")
                                  @RequestParam(value = "recordTime") Date recordTime) {
        HcMedicalEntity entity = new HcMedicalEntity();
        entity.setId(id);
        entity.setMedicalSpecs(medicalSpecs);
        entity.setMedicalFrequency(medicalFrequency);
        entity.setNumbers(numbers);
        entity.setUseType(useType);
        entity.setRecordTime(recordTime);
        hcMedicalService.updateQh(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 根据idCard查询生命特性
     */
    @ApiOperation(value = "查询生命特性信息", notes = "查询生命特性信息")
    @RequestMapping(value = "/vitalCharacteristicsInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes vitalCharacteristicsInfo(VitalCharacteristics entity,
                                           PageParam pageParam) {

        List<VitalCharacteristics> list = vitalCharacteristicsService.queryList(entity, pageParam);
        int total = vitalCharacteristicsService.queryTotal(entity);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("iTotalDisplayRecords", total);
        return new ApiRes(true, map, "", "");
    }

    /**
     * 新增生命特性
     */
    @ApiOperation(value = "新增生命特性", notes = "新增生命特性")
    @RequestMapping(value = "/addvitalCharacteristics", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addvitalCharacteristics(VitalCharacteristics entity) {
        if(StringUtils.isNull(ShiroUtils.getUserId())) {
            return ApiRes.failure("登录失效，请重新登录。");
        }
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(ShiroUtils.getUserId());
        vitalCharacteristicsService.save(entity);
        return ApiRes.SUCCESS;
    }


    /**
     * 跳转生命特性更多页面
     */
    @ApiOperation(value = "生命特性更多页面", notes = "生命特性更多页面")
    @RequestMapping(value = "/toVitalCharacteristics", method = RequestMethod.GET)
    public String toVitalCharacteristics() {
        return "admin/archives/vitalCharacteristics";
    }

    @ApiOperation(value = "删除生命特性")
    @RequestMapping(value = "/deleteVitalCharacteristics", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes deleteVitalCharacteristics(Long[] ids) {
        vitalCharacteristicsService.deleteVitalCharacteristics(ids);
        return ApiRes.SUCCESS;
    }

    /**
     * 跳转至用药信息修改页面
     */
    @ApiOperation(value = "用药信息修改页面", notes = "用药信息修改页面")
    @RequestMapping(value = "/toUpdateVitalCharacteristics", method = RequestMethod.GET)
    public String toUpdateVitalCharacteristics() {
        return "admin/archives/updateVitalCharacteristics";
    }

    /**
     * 修改生命特性
     */
    @ApiOperation(value = "修改生命特性", notes = "修改生命特性")
    @RequestMapping(value = "/updateVitalCharacteristics", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updateVitalCharacteristics(@ApiParam(required = true, name = "id", value = "生命特性id编号")
                                  @RequestParam(value = "id") Long id,
                                  @ApiParam(required = true, name = "temperature", value = "体温")
                                  @RequestParam(value = "temperature") String temperature,
                                  @ApiParam(required = true, name = "pulse", value = "脉搏")
                                  @RequestParam(value = "pulse") String pulse,
                                  @ApiParam(required = true, name = "breath", value = "呼吸")
                                  @RequestParam(value = "breath") String breath,
                                  @ApiParam(required = true, name = "minimumPressure", value = "低血压")
                                  @RequestParam(value = "minimumPressure") String minimumPressure,
                                  @ApiParam(required = true, name = "highestPressure", value = "高血压")
                                  @RequestParam(value = "highestPressure") String highestPressure) {
        VitalCharacteristics entity = new VitalCharacteristics();
        entity.setId(id);
        entity.setTemperature(temperature);
        entity.setPulse(pulse);
        entity.setBreath(breath);
        entity.setMinimumPressure(minimumPressure);
        entity.setHighestPressure(highestPressure);
        vitalCharacteristicsService.updateVitalCharacteristics(entity);
        return ApiRes.SUCCESS;
    }

}
